package net.lab1024.sa.admin.module.business.erp.bas.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.bas.dao.CalendarsDao;
import net.lab1024.sa.admin.module.business.erp.bas.dao.CalendarsaDao;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.CalendarsEntity;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.CalendarsaEntity;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.CalendarsAddForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.CalendarsQueryForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.CalendarsUpdateForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.vo.CalendarsVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 日历 Service
 *
 * @Author AKITA
 * @Date 2025-04-18 08:50:24
 * @Copyright AKITA
 */

@Service
public class CalendarsService {

    @Resource
    private CalendarsDao calendarsDao;
    @Resource
    private CalendarsaDao calendarsaDao;



    Integer num = 0;
    /**
     * 分页查询
     */
    public PageResult<CalendarsVO> queryPage(CalendarsQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<CalendarsVO> list = calendarsDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<Long> add(CalendarsAddForm addForm) {
        CalendarsEntity calendarsEntity = SmartBeanUtil.copy(addForm, CalendarsEntity.class);
        calendarsDao.insert(calendarsEntity);
        return ResponseDTO.ok(calendarsEntity.getDocEntry());
    }

    /**
     * 更新
     */
    @Transactional
    public ResponseDTO<String> update(CalendarsUpdateForm updateForm) {
        CalendarsEntity calendarsEntity = SmartBeanUtil.copy(updateForm, CalendarsEntity.class);
        if ( calendarsDao.updateById(calendarsEntity) == 0) {
            throw new RuntimeException("数据已被更新，刷新后重试！");
        }
        calendarsaDao.deleteById(updateForm.getDocEntry());
        for (CalendarsaEntity calendarsaEntity : updateForm.getLista()) {
            calendarsaDao.insert(calendarsaEntity);
        }
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }
        calendarsDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {
        if (null == docEntry) {
            return ResponseDTO.ok();
        }

        calendarsDao.deleteById(docEntry);
        calendarsaDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<CalendarsEntity> info(Long docEntry) {





        CalendarsEntity calendarsEntity = calendarsDao.selectById(docEntry);
        if (null != calendarsEntity) {
            List<CalendarsaEntity> calendarsaEntities = calendarsaDao.selectList(new LambdaQueryWrapper<CalendarsaEntity>().eq(true, CalendarsaEntity::getDocEntry, docEntry));
            calendarsEntity.setLista(calendarsaEntities);
        }
        return ResponseDTO.ok(calendarsEntity);
    }

    public ResponseDTO<String> excuteService(CalendarsQueryForm queryForm) {
        LocalDate now = LocalDate.now();
        String year = queryForm.getAbsId().toString().substring(0, 4);
        String month = queryForm.getAbsId().toString().substring(4, 6);
        int daysInMonth = YearMonth.of(Integer.valueOf(year), Integer.valueOf(month)).lengthOfMonth();
        String format = String.format("https://timor.tech/api/holiday/year/%s", now.getYear());
        String s = HttpUtil.get(format);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONObject holiday = (JSONObject) jsonObject.get("holiday");

        for (int day = 1; day <= daysInMonth; day++) {
            CalendarsaEntity calendarsaEntity = new CalendarsaEntity();
            calendarsaEntity.setDocEntry(queryForm.getDocEntry());
            calendarsaEntity.setLineNum(day);
            LocalDate date = LocalDate.of(Integer.valueOf(year), Integer.valueOf(month), day);
            calendarsaEntity.setNowDate(date);
            String format1 = date.format(DateTimeFormatter.ofPattern("MM-dd"));
            JSONObject o = (JSONObject) holiday.get(format1);
            DayOfWeek dayOfWeek = date.getDayOfWeek();
            calendarsaEntity.setWeek(dayOfWeek.getValue() + "");
            calendarsaEntity.setAbsId(queryForm.getAbsId());
            if (o != null) {
                calendarsaEntity.setWorkDay(!(Boolean) o.get("holiday"));
                calendarsaEntity.setFestName(o.getString("name"));
                calendarsaEntity.setMultiple(BigDecimal.valueOf(3));
                calendarsaEntity.setDel("c");
            } else if ((calendarsaEntity.getWeek().equals("6") || calendarsaEntity.getWeek().equals("7"))) {
                calendarsaEntity.setWorkDay(false);
                calendarsaEntity.setMultiple(BigDecimal.valueOf(2.0));
                calendarsaEntity.setDel("b");
            } else {
                calendarsaEntity.setWorkDay(true);
                calendarsaEntity.setMultiple(BigDecimal.valueOf(1.5));
                calendarsaEntity.setDel("a");
            }
            calendarsaDao.insert(calendarsaEntity);
        }
        return ResponseDTO.ok();
    }



}
